import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import store from './store'
import {deleteRequest, getRequest, postRequest, putRequest} from "./utils/api";
import {initMenu} from "./utils/menus";

import Print from 'vue-print-nb'
Vue.config.productionTip = false

Vue.use(Print);
Vue.use(ElementUI)

//插件形式使用请求
Vue.prototype.postRequest=postRequest;
Vue.prototype.getRequest=getRequest;
Vue.prototype.putRequest=putRequest;
Vue.prototype.deleteRequest=deleteRequest;

router.beforeEach((to, from, next) => {
  if (window.sessionStorage.getItem("tokenStr")) {
    initMenu(router, store);
    if (!window.sessionStorage.getItem("user")) {
      return getRequest('/getUser').then(resp => {
        if (resp) {
          //存入用户信息
          window.sessionStorage.setItem("user", JSON.stringify(resp));
          store.commit('INIT_USER',resp);
          next({ ...to});
        }
      });
    }
    next();
  } else {
    if (to.path == '/') {
      next();
    } else {
      next('/?redirect=' + to.path)
    }
  }
})

new Vue({
  router,
  store,
  components: {App},
  render: h => h(App)
}).$mount('#app')
